home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / optans10.zip / DEMOANSI.C next >
C/C++ Source or Header  |  1992-06-14  |  13KB  |  460 lines

  1. #include "optansi.h"
  2. #include "auser.h"
  3. #include "skeys.h"
  4. #include <ctype.h>
  5. #include <string.h>
  6. #include <stdio.h>
  7. #include <time.h>
  8. #include <stdlib.h>
  9.  
  10. /* Global variables */
  11. // Counters and Timers
  12. unsigned ocnt, nocnt;
  13. clock_t begin, otime, notime;
  14.  
  15.  
  16.  
  17. pl(char *s) {
  18.     printf(s);
  19.     aupdatexy();
  20.     if(getch()==27)
  21.         exit(1);
  22.     return;
  23. }
  24.  
  25.  
  26. int test1() {
  27. #define MEMBERS 10
  28. // X positions
  29. const char x[]={ 1,  2, 78,  78,  1,  1, 39, 39,  1,  1 };
  30. // Y positions
  31. const char y[]={ 1,  2,  2,  24, 24,  2, 12, 13, 14,  1 };
  32. // Attributes
  33. const char a[]={ 2,  7,  15, 31, 14, 13, 63,191, 79,  7 };
  34. int i, j;
  35.     // Optimizations Count
  36.     acolor(7);
  37.     aclrscr();
  38.     ocnt=0;
  39.     otime=0;
  40.     for (j=0; j<100; j++) {
  41.         for (i=0; i<MEMBERS; i++) {
  42.             begin=clock();
  43.             ocnt+=agotoxy(x[i], y[i]);        // Move cursor to new position
  44.             ocnt+=acolor(a[i]);                        // Set new color
  45.             printf("%X\b", i);                        // Put hex number
  46.             ocnt++;                                                // Add 1 for output of hex character
  47.             otime+=clock()-begin;
  48.             if (!j) {
  49.                 gotoxy(65, 1);                // conio functions used to avoid interfering with ANSI
  50.                 printf("X%d Y%d A%d %X", x[i], y[i], a[i], i);
  51.                 aclreol();
  52.                 if (getch()==27)
  53.                     exit(1);
  54.                 if (i==MEMBERS-1)    {
  55.                     gotoxy(63, 1);
  56.                     atextattr(7);
  57.                     printf("Now 100x repeated");
  58.                     if (getch()==27)
  59.                         exit(1);
  60.                 }
  61.                 gotoxy(x[i], y[i]);        // Restore location
  62.             }
  63.         }
  64.         begin=clock();
  65.         ocnt+=acolor(7);                                // Set color for clear screen to black
  66.         ocnt+=aclrscr();                                // Clear the screen
  67.         otime+=clock()-begin;
  68.     }
  69.  
  70.     // Non-optimizations Count
  71.     atextattr(7);
  72.     aclrscr();
  73.     gotoxy(63, 1);
  74.     pl("Now Nonoptimized");
  75.     nocnt=0;
  76.     notime=0;
  77.     for (j=0; j<100; j++) {
  78.         for (i=0; i<MEMBERS; i++) {
  79.             begin=clock();
  80.             nocnt+=oldgotoxy(x[i], y[i]);    // Move cursor to new position
  81.             nocnt+=oldcolor(a[i]);                // Set new color
  82.             printf("%X\b", i);                        // Put hex number
  83.             nocnt++;                                            // Add 1 for output of hex character
  84.             notime+=clock()-begin;
  85.         }
  86.         begin=clock();
  87.         nocnt+=oldcolor(7);                            // Set color for clear screen to black
  88.         nocnt+=oldclrscr();                            // Clear the screen
  89.         notime+=clock()-begin;
  90.     }
  91.     return;
  92. }
  93.  
  94.  
  95. int test2() {
  96.     int i, j;
  97.     struct point {
  98.         char x, y, a;
  99.     } *ptr, *st;
  100.  
  101.     if ((st = malloc(100*MEMBERS*sizeof(struct point))) == NULL)
  102.     {
  103.          printf("Not enough memory for Test 2\n");
  104.          return(0);
  105.     }
  106.     ptr=st;
  107.  
  108.  
  109.     // Optimizations Count
  110.     randomize();
  111.     acolor(7);
  112.     aclrscr();
  113.     ocnt=0;
  114.     otime=0;
  115.     for (j=0; j<100*MEMBERS; j++) {
  116.         ptr->x=random(80)+1;
  117.         ptr->y=random(24)+1;
  118.         ptr->a=random(256);
  119.  
  120.         begin=clock();
  121.         ocnt+=agotoxy(ptr->x, ptr->y);        // Move cursor to new position
  122.         ocnt+=acolor(ptr->a);                            // Set new color
  123.         ocnt+=printf("%X", j);                        // Put hex number
  124.         aupdatexy();
  125.         otime+=clock()-begin;
  126.  
  127.         ptr++;
  128.     }
  129.  
  130.     ptr=st;
  131.     // Non-optimizations Count
  132.     gotoxy(63, 1);
  133.     atextattr(7);
  134.     pl("Now Nonoptimized");
  135.     aclrscr();
  136.     nocnt=0;
  137.     notime=0;
  138.     for (j=0; j<100*MEMBERS; j++) {
  139.         begin=clock();
  140.         nocnt+=oldgotoxy(ptr->x, ptr->y);    // Move cursor to new position
  141.         nocnt+=oldcolor(ptr->a);                    // Set new color
  142.         nocnt+=printf("%X", j);                        // Put hex number
  143.         notime+=clock()-begin;
  144.  
  145.         ptr++;
  146.     }
  147.     free(st);
  148.     return(1);
  149. }
  150.  
  151.  
  152. test3() {
  153.     char s[25];
  154.     unsigned i;
  155.  
  156.     /* LOCATION FUNCTIONS */
  157.     aclrscr();
  158.     pl("LOCATION FUNCTIONS:  aclrscr()\n");
  159.     pl("aupdatexy() - updates axpos and aypos after a printf\n");
  160.     aupdatexy();
  161.     pl("aupdatexy1() - same as aupdatexy except that it uses low memory to get values\n");
  162.     aupdatexy1();
  163.     ahome();            /* MAJOR BUG HERE */
  164.     pl("ahome()");
  165.     aclreol();
  166.     pl("");
  167.     pl(" aclreol() - Note how everything disappeared to the right?");
  168.     adown(5);
  169.     pl("adown(5)");
  170.     aleft(25);
  171.     pl("aleft(25)");
  172.     aup(2);
  173.     pl("aup(2)");
  174.     aright(7);
  175.     pl("aright(7)");
  176.     agotoxy(1,12);
  177.     pl("agotoxy(1,12)");
  178.     pl("\nasavecursor() - Note the cursor location now ->");
  179.     asavecursor();
  180.     // Note this next line uses oldgotoxy which does not update the global variables axpos and aypos
  181.     oldgotoxy(1,24);
  182.     pl("gotoxy(1,24) - This goto uses some other goto function that doesn't update axpos  and aypos so aforcegotoxy can help to resolve");
  183.     aforcegotoxy(1,12);
  184.     pl("aforcegotoxy(1,12)");
  185.     arestorecursor();
  186.     pl("X\narestorecursor() - location is restored to where the X above is placed");
  187.  
  188.     /* COLOR FUNCTIONS */
  189.     aclrscr();
  190.     pl("COLOR FUNCTIONS:\n");
  191.     acolor(ATTR(YELLOW, GREEN));
  192.     pl("acolor(ATTR(YELLOW,GREEN))\n");
  193.     atextattr(ATTR(WHITE,BLUE));
  194.     pl("atextattr(ATTR(WHITE,BLUE))  - same as acolor\n");
  195.     atextcolor(LIGHTRED);
  196.     pl("atextcolor(LIGHTRED) - foreground color\n");
  197.     atextbackground(RED);
  198.     pl("atextbackground(RED) - background color\n");
  199.     aresetcolor();
  200.     pl("aresetcolor()\n");
  201.     ahighlight();
  202.     pl("ahighlight() or ahighvideo()\n");
  203.     ablink();
  204.     pl("ablink()\n");
  205.     anohighlight();
  206.     pl("anohighlight() or alowvideo()\n");
  207.     //    anormvideo()
  208.     anoblink();
  209.     pl("anoblink()\n\nThese next 3 functions are special ANSI color commands and behave differently\n  on different emulators. See DOCS.\n");
  210.     aunderline(;);
  211.     pl("aunderline(equation)   ");
  212.     areverse(;);
  213.     pl("areverse(equation)   ");
  214.     ainvisible(;);
  215.     pl("ainvisible(equation)\n");
  216.     oldcolor(7);
  217.     printf("That last color was ainvisible() in case you could not see it.\n\n");
  218.     pl("The color has been changed using a function that doesn't update acurattr,\n  so aforcecolor can be used to resolve\n");
  219.     aforcecolor(ATTR(WHITE,CYAN));
  220.     pl("aforcecolor(ATTR(WHITE,CYAN))\n");
  221.  
  222.     /* MISCELLANEOUS FUNCTIONS */
  223.     acolor(7);
  224.     aclrscr();
  225.     pl("MISCELLANEOUS FUNCTIONS:\n");
  226.  
  227.     printf(
  228.         "Multiple test methods that test for ANSI presence -\n"
  229.         "atest()\n"
  230.         "  aresult():  "
  231.     );
  232.     atest();
  233.     for (i=0;kbhit();i++)        // read input buffer
  234.         s[i]=getch();
  235.     if (aresult(s))
  236.         printf("TRUE\n");
  237.     else
  238.         printf("FALSE\n");
  239.  
  240.     printf(
  241.         "atest1()\n"
  242.         "  aresult1(): "
  243.     );
  244.     atest1();
  245.     if (aresult1())
  246.         printf("TRUE\n");
  247.     else
  248.         printf("FALSE\n");
  249.  
  250.     printf("aresult2():   ");
  251.     if (aresult2())
  252.         printf("TRUE\n");
  253.     else
  254.         printf("FALSE\n");
  255.  
  256.     atest();
  257.     for (i=0;kbhit();i++)        // read input buffer
  258.         s[i]=getch();
  259.     i=awherexy(s);
  260.     printf("awherexy():   Cursor at X:%d Y:%d at beginning of this line\n\n", i/256, i%256);
  261.  
  262.     pl("akeyboard() will allow you to redefine keyboard or create macros if your ANSI\n  emulator supports them.\n");
  263.     pl("asetmode() and aresetmode() changes screen modes and variable options. Watch.\n\nasetmode(CO40)");
  264.     asetmode(C40);
  265.     pl("now back to asetmode(C80)");
  266.     asetmode(C80);
  267.     pl("\n\n\n\nanocursorwrap() - turns off cursor wrap\n");
  268.     anocursorwrap();
  269.     // These printf strings exceed 80 columns.
  270.     printf("01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n");
  271.     pl("acursorwrap() - turns it back on.. Below is the same string as it should have\n  appeared.\n");
  272.     acursorwrap();
  273.     printf("01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n");
  274.  
  275.     pl("That concludes DEMOANSI tests.. ");
  276.  
  277.     return;
  278. }
  279.  
  280.  
  281. results(int testnum) {
  282.  
  283.     aforcegotoxy(57, 1);
  284.     aforcecolor(7);
  285.     pl("Press a key for results");
  286.  
  287.     aclrscr();
  288.     printf("TEST %d\n", testnum);
  289.     printf("\nOPTIMIZED ANSI STATISTICS (OPTANSI)\n");
  290.     printf(
  291.         "Total number of characters counted in 100 repetitions of ANSI output: %5d\n"
  292.         "At 2400 baud that would equate to %2.2f seconds.\n"
  293.         "At 9600 baud that would equate to %2.2f seconds.\n"
  294.         "Time of operations on this computer was %2.4f seconds.\n",
  295.         ocnt, ocnt/240.0, ocnt/960.0, (float) otime/CLK_TCK);
  296.     printf("\nNON-OPTIMIZED ANSI STATISTICS (OTHER LIBRARIES)\n");
  297.     printf(
  298.         "Total number of characters counted in 100 repetitions of ANSI output: %5d\n"
  299.         "At 2400 baud that would equate to %2.2f seconds.\n"
  300.         "At 9600 baud that would equate to %2.2f seconds.\n"
  301.         "Time of operations on this computer was %2.4f seconds.\n",
  302.         nocnt, nocnt/240.0, nocnt/960.0, (float) notime/CLK_TCK);
  303.     printf("\nRELATIONAL IMPROVEMENT WHEN USING OPTANSI\n");
  304.     printf(
  305.         "Percent of output reduced using OptAN